import PcPreview from './pcPreview.js';
import PhonePreview from './phonePreview.js';

const handleUrl = (url = '') => {
    if (url.startsWith('//')) {
        url = `https:${url}`;
    }
    // todo 如果地址是以cdn.weipaitang开头的要添加https://
    return url.replace(/\/w\/[0-9]+$/, '');
};

export default function previewImage(data) {
    if (data.current) {
        data.current = handleUrl(data.current);
    }
    
    if (!data.urls) {
        data.urls = [data.current];
    }

    data.urls = (data.urls || []).map(url => handleUrl(url));

    let wxwork = !!navigator.userAgent.toLowerCase().match('wxwork');
    if (wxwork) {
        WPT.os.device = 'wxwork';
    }

    if (WPT.os.device === 'native' || (WPT.os.device === 'wechat' && $.os.phone === true)) {
        window.wx && window.wx.previewImage && window.wx.previewImage(data);
    } else if (WPT.os.device === 'weibo') {
        window.WeiboJS && window.WeiboJS.invoke('openImage', { url: data.current, urls: data.urls });
    } else if (WPT.os.device === 'web-phone' || WPT.os.device === 'wxwork') {
        PhonePreview(data);
    } else if (WPT.os.device === 'web-pc') {
        PcPreview(data);
    } else {
        PcPreview(data);
    }
}
